/*****************************************************************************
文 件 名: atcommand.c
描    述: sim900的AT命令
作    者: 武玉峰
版    本: 1.0
完成日期: 2016.8.19
修改历史: 
*****************************************************************************/

#include "string.h"
#include "stm32f10x.h"
#include "atcommand.h"
#include "usart.h"
#include "stdlib.h"
#include "wdg.h"

extern u8        _UART1Buffer[RECEIVED_BUFFER_SIZE];
extern u16       _TimeOutCount;
extern u16       _UART1BufferPosition;
extern u8         _IMEI[16];
u16 StrLen(u8 * str)
{
    int i=0;
    while(*str!='\0')
    {
        ++str;
        ++i;
    }
    return i;

}

//判断一个字符串是否以另一个字符串开始
u8 StartWith(u8 * pSrc,u8 *pMatch)
{
    u16 srclen= StrLen(pSrc);
    u16 matchlen=StrLen(pMatch);
    u16 i=0;
    if(srclen<matchlen)return 0;

    while(i<matchlen)
    {
        if(pMatch[i]!=pSrc[i])
            return 0;
        ++i;
    }
    return 1;
}

//判断一个字符串是否以另一个字符串结尾
u8  EndWith(u8 * pSrc,u8 *pMatch)
{
    int srclen= StrLen(pSrc);
    int matchlen=StrLen(pMatch);

    if(srclen<matchlen)return 0;
    while(matchlen>0)
    {
        if(pMatch[matchlen-1]!=pSrc[srclen-1])
            return 0;
        matchlen--;
        srclen--;
    }

    return 1;
}




char AT()
{

    char  command[] = "AT\r\n";
    char *p;
    
    //发送AT命令

    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
    _UART1BufferPosition=0;
    p=command;

    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }



    //读取命令响应
    //初始化缓冲区

    _TimeOutCount=0;
    while(1)
    {
			 IWDG_Feed();	
        if(EndWith(_UART1Buffer,(u8 *)"OK\r\n"))
        {
            _UART1BufferPosition=0;
            return 1;
        }
        //AT命令的超时等待要长一些，因为要等待Sim900启动
        if(_TimeOutCount>15)
        {
					DebugOut("%s\r\n",_UART1Buffer);
            //超时 6秒
            return 0;
        }
    }
//    return 0;
}


char CIPCLOSE()
{

    char  command[] = "AT+CIPCLOSE\r\n";
    char *p;
    
    //发送AT命令

    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
    _UART1BufferPosition=0;
    p=command;

    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }



    //读取命令响应
    //初始化缓冲区

    _TimeOutCount=0;
    while(1)
    {
			 IWDG_Feed();	
        if(EndWith(_UART1Buffer,(u8 *)"CLOSE OK\r\n"))
        {
            _UART1BufferPosition=0;
            return 1;
        }
				  if(EndWith(_UART1Buffer,(u8 *)"CLOSED\r\n"))
        {
            _UART1BufferPosition=0;
            return 1;
        }
        //AT命令的超时等待要长一些，因为要等待Sim900启动
        if(_TimeOutCount>15)
        {
            //超时 6秒
					  DebugOut("%s\r\n",_UART1Buffer);
            return 0;
        }
    }
//    return 0;
}
 
////查询sim900当前波特率
//u32 CSMINS()
//{
//    u8 command[] = "AT+CSMINS?\r\n";
//    u8 *p=command;

//    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
//    _UART1BufferPosition=0;
//    //发送AT命令
//    while(*p!='\0')
//    {
//        USART_SendData(USART1, *p);//向串口1发送数据
//        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
//        ++p;
//    }

//    _TimeOutCount=0;
//    while(1)
//    {
//			 IWDG_Feed();	
//        if(EndWith(_UART1Buffer,(u8 *)"OK\r\n"))
//        {
//            _UART1BufferPosition=0;


//            return 1;

//        }
//        if(EndWith(_UART1Buffer,(u8 *)"ERROR\r\n"))
//        {
//            _UART1BufferPosition=0;
//            return 0;
//        }
//        if(_TimeOutCount>6)
//        {
//					DebugOut("%s\r\n",_UART1Buffer);
//            //超时 6秒
//            _UART1BufferPosition=0;
//            return 0;
//        }
//    }
////    _UART1BufferPosition=0;
////    return 0;
//}
//设置sim900当前波特率为9600
u32 IPR()
{
    u8 command[] ="AT+IPR=9600\r\n";
	  //u8 command[] ="AT+IPR=115200\r\n";
    u8 *p=command;

    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
    _UART1BufferPosition=0;
    //发送AT命令
    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }

    _TimeOutCount=0;
    while(1)
    {
			 IWDG_Feed();	
        if(EndWith(_UART1Buffer,(u8 *)"OK\r\n"))
        {
            _UART1BufferPosition=0;

            return 1;

        }
        if(EndWith(_UART1Buffer,(u8 *)"ERROR\r\n"))
        {
            _UART1BufferPosition=0;
            return 0;
        }
        if(_TimeOutCount>6)
        {
					DebugOut("%s\r\n",_UART1Buffer);
            //超时 6秒
            _UART1BufferPosition=0;
            return 0;
        }
    }
 
}

char IFC()
{

    char command[] = "AT+IFC=2,2\r\n";
	  //char command[] = "AT+IFC=0,0\r\n";
    char *p=command;
   
    //发送AT命令
    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
    _UART1BufferPosition=0;


    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }

    _TimeOutCount=0;
    while(1)
    {
			 IWDG_Feed();	
        if(EndWith(_UART1Buffer,"OK\r\n"))
        {
            _UART1BufferPosition=0;
            return 1;
        }
        if(_TimeOutCount>10)
        {
					DebugOut("%s\r\n",_UART1Buffer);
            //超时 6秒
            return 0;
        }
    }
//    return 0;
}



 

char   CIPSHUT()
{
    char command[] = "AT+CIPSHUT\r\n";
    char *p=command;
    //发送AT命令
    //初始化缓冲区
    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
    _UART1BufferPosition=0;

    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }

    //读取命令响应

    _TimeOutCount=0;
    while(1)
    {
			 IWDG_Feed();	
        if(EndWith(_UART1Buffer, (u8 *)"SHUT OK\r\n"))
        {
            _UART1BufferPosition=0;
            return 1;
        }
        if(EndWith(_UART1Buffer, (u8 *)"ERROR\r\n"))
        {
            _UART1BufferPosition=0;
            return 0;
        }
        if(_TimeOutCount>10)
        {
					DebugOut("%s\r\n",_UART1Buffer);
            //超时 6秒
            _UART1BufferPosition=0;
            return 0;
        }
    }
//    _UART1BufferPosition=0;
//    return 0;
}

//查询信号质量
u8   CSQ(void)
{
	  u8 sigint=0;
	  int k=0;
    char command[] = "AT+CSQ\r\n";
    char *p=command;
    //发送AT命令
    //初始化缓冲区
    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
    _UART1BufferPosition=0;

    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }

    //读取命令响应

    _TimeOutCount=0;
    while(1)
    {
			 IWDG_Feed();	
        if(EndWith(_UART1Buffer, (u8 *)"OK\r\n"))
        {
					
					//取出返回的结果
			
					for(k=0;k<_UART1BufferPosition;++k)
					{
						if(_UART1Buffer[k]==':')
							break;
					}
          sigint=atoi((const char *)&_UART1Buffer[k+1]);
            _UART1BufferPosition=0;
            return sigint;
        }
        if(_TimeOutCount>10)
        {
					DebugOut("%s\r\n",_UART1Buffer);
            //超时 6秒
            _UART1BufferPosition=0;
            return 0;
        }
    }

}
char CIPMODE()
{
    char command[] = "AT+CIPMODE=1\r\n";
    char *p=command;
    //发送AT命令
    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
    _UART1BufferPosition=0;

    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }

    //读取命令响应
    //初始化缓冲区

    _TimeOutCount=0;
    while(1)
    {
			 IWDG_Feed();	
        if(EndWith(_UART1Buffer,"OK\r\n"))
        {
            _UART1BufferPosition=0;
            return 1;
        }
        if(EndWith(_UART1Buffer,"ERROR\r\n"))
        {
            _UART1BufferPosition=0;
            return 0;
        }
        if(_TimeOutCount>10)
        {
					DebugOut("%s\r\n",_UART1Buffer);
            //超时 6秒
            _UART1BufferPosition=0;
            return 0;
        }
    }
//    _UART1BufferPosition=0;
//    return 0;
}




char CIPSTART(u8 * serverip,u8 * port)
{

    char   command[64];
    // char code format[]={"AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n"};
    char  format[]="AT+CIPSTART=\"TCP\",\"";
    int i=0;
    int strlen=0;
    char *p=command;
    int k=0;
    memset(command,'\0',sizeof(command));
    strlen=19; //StrLen(format);
    for(i=0; i<strlen; ++i,++k)
        command[k]=format[i];
    //"218.25.125.149"
    strlen=StrLen(serverip);
    for(i=0; i<strlen; ++i,++k)
        command[k]=serverip[i];

    command[k++]='"';
    command[k++]=',';
    command[k++]='"';

    //"8000"
    strlen=StrLen(port);
    for(i=0; i<strlen; ++i,++k)
        command[k]=port[i];
    command[k++]='"';
    command[k++]='\r';
    command[k++]='\n';
    //sprintf(command,format,serverip,port);
    p=command;

    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));

    _UART1BufferPosition=0;

    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }


    _TimeOutCount=0;
    while(1)
    {
			 IWDG_Feed();	
        if(EndWith(_UART1Buffer,"CONNECT OK\r\n")||EndWith(_UART1Buffer,"CONNECT\r\n"))
        {
            _UART1BufferPosition=0;
            return 1;
        }

        if(EndWith(_UART1Buffer,"ERROR\r\n"))
        {
            _UART1BufferPosition=0;
            return 0;
        }

        if(EndWith(_UART1Buffer,"CONNECT FAIL\r\n"))
        {
            _UART1BufferPosition=0;
            return 0;
        }

        if(EndWith(_UART1Buffer,"PDP: DEACT\r\n"))
        {
            _UART1BufferPosition=0;
            return 1;
        }

        if(EndWith(_UART1Buffer,"STATE: IP IND\r\n"))
        {
            _UART1BufferPosition=0;
            return 0;
        }
        if(EndWith(_UART1Buffer,"CLOSED\r\n"))
        {
            _UART1BufferPosition=0;
            return 0;
        }





        if(_TimeOutCount>10)
        {
					DebugOut("%s\r\n",_UART1Buffer);
            //超时 6秒   +
            _UART1BufferPosition=0;
            return 0;
        }
    }
//    _UART1BufferPosition=0;
//    return 0;
}

//获取IMEI号
char  GetIMEI()
{

    char command[] = "AT+CGSN\r\n";
    char *p=command;
    //发送AT命令
    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
    memset(_IMEI,'\0',sizeof(_IMEI));
    _UART1BufferPosition=0;

    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }

    _TimeOutCount=0;

    while(1)
    {
			 IWDG_Feed();	
        if(EndWith(_UART1Buffer,"OK\r\n"))
        {
            _UART1BufferPosition=0;
            //	extern void *memcpy (void *s1, void *s2, int n);
             memcpy(_IMEI,&_UART1Buffer[11],15);
					
				  	 //memcpy(_IMEI,"869668023433190",15);
            return 1;
        }

        if(_TimeOutCount>10)
        {
					DebugOut("%s\r\n",_UART1Buffer);
            //超时 6秒
            return 0;
        }
    }
//    return 0;
}


char CIPSTATUS()
{
    char   command[] = "AT+CIPSTATUS\r\n";
    char *p=command;
    //发送AT命令
    memset(_UART1Buffer,'\0',sizeof(_UART1Buffer));
    _UART1BufferPosition=0;

    while(*p!='\0')
    {
        USART_SendData(USART1, *p);//向串口1发送数据
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
        ++p;
    }


    _TimeOutCount=0;

    while(1)
    {
        //0     IP INITIAL          初始化
        //1     IP START           启动任务
        //2     IP CONFIG               配置场景
        //3     IP IND                   接受场景配置
        //4     IP GPRSACT              场景已激活
        //5     IP STATUS          获得本地 IP地址
        //6     TCP/UDP CONNECTING   与 SERVER 建立连接阶段
        //7     IP CLOSE                连接已关闭
        //8     CONNECT OK         连接建立成功
 IWDG_Feed();	
        if (EndWith(_UART1Buffer,"IP INITIAL\r\n")) {
            return IP_INITIAL;
        }
        if (EndWith(_UART1Buffer,"IP START\r\n")) {
            return IP_START;
        }
        if (EndWith(_UART1Buffer,"IP CONFIG\r\n")) {
            return IP_CONFIG;
        }
        if (EndWith(_UART1Buffer,"IP IND\r\n")) {
            return IP_IND;
        }
        if (EndWith(_UART1Buffer,"IP GPRSACT\r\n")) {
            return IP_GPRSACT;
        }
        if (EndWith(_UART1Buffer,"IP STATUS\r\n")) {
            return IP_STATUS;
        }
        if (EndWith(_UART1Buffer,"TCP CONNECTING\r\n")) {
            return TCP_CONNECTING;
        }
        if (EndWith(_UART1Buffer,"IP CLOSE\r\n")) {
            return IP_CLOSE;
        }
        if (EndWith(_UART1Buffer,"CONNECT OK\r\n")) {
            return CONNECT_OK;
        }
        if (EndWith(_UART1Buffer,"PDP DEACT\r\n")) {
            return PDP_DEACT;
        }
        if (EndWith(_UART1Buffer,"TCP CLOSED\r\n")) {
            return TCP_CLOSED;
        }

    }
}





